package com.sgb.zhaocai.view.demand.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.tabs.TabLayout;
import com.sgb.link.entity.EventConstant;
import com.sgb.link.entity.LoginEvents;
import com.sgb.zhaocai.R;
import com.sgb.zhaocai.databinding.ZcDemandForecastActBinding;
import com.sgb.zhaocai.entity.LabourWorkListEntity;
import com.sgb.zhaocai.entity.LeaseNeedListEntity;
import com.sgb.zhaocai.entity.MaterialListEntity;
import com.sgb.zhaocai.entity.ProjectListEntity;
import com.sgb.zhaocai.entity.RequestBean;
import com.sgb.zhaocai.entity.SubpackagePlanListEntity;
import com.sgb.zhaocai.utils.PickCopyUtils;
import com.sgb.zhaocai.view.demand.adapter.LabourWorkAdapter;
import com.sgb.zhaocai.view.demand.adapter.LeaseNeedAdapter;
import com.sgb.zhaocai.view.demand.adapter.MaterialsBuyAdapter;
import com.sgb.zhaocai.view.demand.adapter.ProjectAdapter;
import com.sgb.zhaocai.view.demand.adapter.SubpackagePlanAdapter;
import com.sgb.zhaocai.utils.PickUtil;
import com.sgb.zhaocai.widget.threefliter.ZCThreeLevelFilterWindow;
import com.sgb.zhaocai.viewmodel.DemandForecastActModel;
import com.swgk.core.base.AppActivity;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.util.CommonUtils;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.view.areapopup.AddressEntity;
import com.swgk.core.view.areapopup.GoodsSourceLocationEntity;
import com.swgk.core.view.areapopup.ThreeLevelAreaWindow;
import com.swgk.core.view.selection.CommonPopEntity;
import com.swgk.core.view.selection.MemberStatusView;
import com.swgk.core.xrecyclerview.XRecyclerView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * Time:2021/7/1
 * AUTHOR:F_Q
 * Description: 需求预告
 */
public class DemandForecastActivity extends AppActivity implements MemberStatusView.OnChooseListener {

    private ZcDemandForecastActBinding mBinding;
    private String[] mTitles = {"工程项目", "材料采购", "机械租赁", "设备租赁", "劳务用工", "分包计划"};
    private RequestBean requestBean = new RequestBean();
    private String longitude = "";
    private String latitude = "";
    //地址选择
    private ThreeLevelAreaWindow mAddressPop;
    private String province = "";
    private String city = "";
    private int position = 0;
    //地址选择list
    private List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> scrData;
    //排序
    private String sort = "";
    //工程类型
    private String projectType = "";
    //时间
    private String startTime = "";
    private boolean isLoadMore = false;
    private DemandForecastActModel mModel;
    private ProjectAdapter projectadapter;
    private MaterialsBuyAdapter materialsadapter;
    private LeaseNeedAdapter leaseneedadapter;
    private LabourWorkAdapter labourworkadapter;
    private SubpackagePlanAdapter subpackageplanadapter;
    private ZCThreeLevelFilterWindow popCategory;
    private String mNo1 = "";
    private String mNo3 = "";
    private String mNo2 = "";
    private List<CommonPopEntity> commonlist;
    private String typeName1;


    public static void start(Context context) {
        Intent intent = new Intent(context, DemandForecastActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    protected void initView() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.zc_demand_forecast_act);
        mModel = new ViewModelProvider(this).get(DemandForecastActModel.class);
        mBinding.ivBack.setOnClickListener(v -> finish());
        viewData();
    }

    private void viewData() {
        //菜单处理
        mTitlesData();
        initviewData();
        //adapter处理
        adapterView(0);
        //初始化popview
        initializePopView();
    }

    private void adapterView(int adapterNum) {
        mBinding.etView.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() > 0) {
                    mBinding.tvSearchDelete.setVisibility(View.VISIBLE);
                }
            }
        });
        mBinding.tvSearchDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mBinding.etView.setText("");
                mBinding.tvSearchDelete.setVisibility(View.GONE);
                initDataList();
            }
        });
        mBinding.tvSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initDataList();
            }
        });
        mBinding.rightView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (CommonUtils.buttonClick()) {
                    return;
                } else {
                    PickUtil.showBottomZhaoCaiDialog(DemandForecastActivity.this);
                }
            }
        });
        //工程项目adapter
        if (projectadapter == null) {
            projectadapter = new ProjectAdapter(getActivity(), null);
        }
        projectadapter.setOnItemClick(new AdapterOnItemClick<ProjectListEntity>() {
            @Override
            public void onItemClick(ProjectListEntity entity, int position) {
                mModel.getH5Url(getActivity(), entity.getId(), longitude, latitude, "engineeringProject");
            }
        });
        //材料采购adapter
        if (materialsadapter == null) {
            materialsadapter = new MaterialsBuyAdapter(getActivity(), null);
        }
        //租赁需求adapter
        if (leaseneedadapter == null) {
            leaseneedadapter = new LeaseNeedAdapter(getActivity(), null);
        }
        //劳务用工adapter
        if (labourworkadapter == null) {
            labourworkadapter = new LabourWorkAdapter(getActivity(), null);
        }
        //分包计划adapter
        if (subpackageplanadapter == null) {
            subpackageplanadapter = new SubpackagePlanAdapter(getActivity(), null);
        }
        subpackageplanadapter.setOnItemClick(new AdapterOnItemClick<SubpackagePlanListEntity>() {
            @Override
            public void onItemClick(SubpackagePlanListEntity entity, int position) {
                mModel.getH5Url(getActivity(), entity.getId(), longitude, latitude, "subcontractPlan");
            }
        });
        //材料我要报价
        materialsadapter.setOnItemStateOperate(new MaterialsBuyAdapter.OnItemStateOperate() {
            @Override
            public void itemOffer(MaterialListEntity entity) {
                PickUtil.startDemandOffer(DemandForecastActivity.this, entity.getId(),entity.getType(),entity.getCompNo(),"2");
            }
        });
        //机械和设备我要报价
        leaseneedadapter.setOnItemStateOperate(new LeaseNeedAdapter.OnItemStateOperate() {
            @Override
            public void itemOffer(LeaseNeedListEntity entity) {
                PickUtil.startDemandOffer(DemandForecastActivity.this, entity.getId(),entity.getType(),entity.getCompNo(),"2");
            }
        });
        //劳务我要报价
        labourworkadapter.setOnItemStateOperate(new LabourWorkAdapter.OnItemStateOperate() {
            @Override
            public void itemOffer(LabourWorkListEntity entity) {
                PickUtil.startDemandOffer(DemandForecastActivity.this, entity.getId(),entity.getType(),entity.getCompNo(),"2");
            }
        });
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(LoginEvents event) {
        if (event == null) {
            return;
        }
        if (event.getMessageTag() == EventConstant.EVENT_LOGIN_OUT||event.getMessageTag() == EventConstant.EVENT_LOGIN) {
            longitude = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LONGITUDE_ID, "");
            latitude = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LATITUDE_ID, "");
            if (TextUtils.isEmpty(longitude)) {
                longitude = "0";
            }
            if (TextUtils.isEmpty(latitude)) {
                longitude = "0";
            }
            //列表新消息
            listData();
            initDataList();
        }
    }


    private void initviewData() {
        mBinding.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                mBinding.startTime.refreshChoose("", mBinding.startTime.getKeyName(), 0);
                mBinding.sort.refreshChoose("", mBinding.sort.getKeyName(), 0);
                mBinding.projectType.refreshChoose("", mBinding.projectType.getKeyName(), 0);
                mBinding.district.refreshChoose("", mBinding.district.getKeyName(), 0);
                mBinding.type.refreshChoose("", mBinding.type.getKeyName(), 0);
                city = "";
                province = "";
                if (scrData != null) {
                    String ProvinceCode = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.AREA_CODE, "");
                    if (!TextUtils.isEmpty(ProvinceCode)) {
                        mAddressPop.setData(scrData, true, ProvinceCode);
                    } else {
                        mAddressPop.setData(scrData, true);
                    }
                }
                mBinding.startTime.clearData();
                mBinding.sort.clearData();
                mBinding.projectType.clearData();
                requestBean = new RequestBean();
                position = tab.getPosition();
                mNo1 = "";
                mNo2 = "";
                mNo3 = "";
                requestBean.setCategoryId("");
                requestBean.setSortMethod("1");
                switch (position) {
                    case 1://材料
                        mModel.getMaterialType(DemandForecastActivity.this);
                        break;
                    case 2://机械
                        mModel.getMechanicalType(DemandForecastActivity.this);
                        break;
                    case 3://设备
                        mModel.getEquipmentType(DemandForecastActivity.this);
                        break;
                }
                //更新view信息
                listData();
                //列表数据
                initDataList();
                if (tab != null && tab.getCustomView() != null) {
                    TextView tabSelect = tab.getCustomView().findViewById(R.id.tv_tab_item);
                    ImageView imageView = tab.getCustomView().findViewById(R.id.image_tab_item);
                    PickCopyUtils.setDemandForecastModel(tab.getPosition(), imageView, true, getActivity());
                    tabSelect.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                    tabSelect.setTextColor(getResources().getColor(R.color.color_main));
                    tabSelect.invalidate();
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                if (tab != null && tab.getCustomView() != null) {
                    TextView tabSelect = tab.getCustomView().findViewById(R.id.tv_tab_item);
                    ImageView imageView = tab.getCustomView().findViewById(R.id.image_tab_item);
                    PickCopyUtils.setDemandForecastModel(tab.getPosition(), imageView, false, getActivity());
                    tabSelect.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                    tabSelect.setTextColor(getResources().getColor(R.color.color_tab_one));
                    tabSelect.invalidate();
                }
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private void initDataList() {
        requestBean.setLocalLat(latitude + "");
        requestBean.setLocalLng(longitude + "");
        isLoadMore = false;
        requestBean.page = 1;
        if (!TextUtils.isEmpty(mBinding.etView.getText().toString())) {
            requestBean.setKeyword(mBinding.etView.getText().toString());
        } else {
            requestBean.setKeyword("");
        }
        mModel.getMaterialHomeList(this, position, requestBean);
    }

    private void listData() {
        switch (position) {
            case 0:
                mBinding.startTime.setKeyName("创建时间");
                mBinding.projectType.setVisibility(View.VISIBLE);
                mBinding.type.setVisibility(View.GONE);
                break;
            case 1:
            case 2:
            case 3:
                mBinding.startTime.setKeyName("使用时间");
                mBinding.projectType.setVisibility(View.GONE);
                mBinding.type.setVisibility(View.VISIBLE);
                break;
            case 4:
            case 5:
                mBinding.startTime.setKeyName("开工时间");
                mBinding.projectType.setVisibility(View.VISIBLE);
                mBinding.type.setVisibility(View.GONE);
                break;
        }
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        mBinding.recyclerView.setLayoutManager(layoutManager);
        switch (position) {
            case 0:
                mBinding.recyclerView.setAdapter(projectadapter);
                break;
            case 1:
                mBinding.recyclerView.setAdapter(materialsadapter);
                break;
            case 2:
                mBinding.recyclerView.setAdapter(leaseneedadapter);
                break;
            case 3:
                mBinding.recyclerView.setAdapter(leaseneedadapter);
                break;
            case 4:
                mBinding.recyclerView.setAdapter(labourworkadapter);
                break;
            case 5:
                mBinding.recyclerView.setAdapter(subpackageplanadapter);
                break;
        }
        mBinding.recyclerView.setHasFixedSize(true);
        //解决数据加载完成后, 没有停留在顶部的问题
        mBinding.recyclerView.setFocusable(false);
        mBinding.recyclerView.setLoadingMoreEnabled(true);
        mBinding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                initDataList();
            }

            @Override
            public void onLoadMore() {
                isLoadMore = true;
                requestBean.page++;
                mModel.getMaterialHomeList(DemandForecastActivity.this, position, requestBean);
            }
        });
        mBinding.recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull @NotNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();//获取LayoutManager
                if (manager != null && manager instanceof LinearLayoutManager) {
                    //第一个可见的位置
                    int firstPosition = ((LinearLayoutManager) manager).findFirstVisibleItemPosition();
                    Log.e("tag数据", firstPosition + "");
                    //如果 dx>0 则表示 右滑 ,dx<0 表示 左滑,dy <0 表示 上滑, dy>0 表示下滑
                    if (dy < 0) {
                        //上滑监听
                        mBinding.imgBackTop.setVisibility(firstPosition > 4 ? View.VISIBLE : View.GONE);
                    } else {
                        //下滑监听
                        mBinding.imgBackTop.setVisibility(firstPosition == 1 ? View.GONE : View.VISIBLE);
                    }
                }
            }
        });
        mBinding.imgBackTop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mBinding.recyclerView.smoothScrollToPosition(0);
            }
        });
    }

    @Override
    protected void init() {
        super.init();
        longitude = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LONGITUDE_ID, "");
        latitude = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LATITUDE_ID, "");
        if (TextUtils.isEmpty(longitude)) {
            longitude = "0";
        }
        if (TextUtils.isEmpty(latitude)) {
            longitude = "0";
        }
        //列表新消息
        listData();
        initDataList();
    }

    /**
     * 工程项目
     *
     * @param list
     * @param isLastPage
     */
    public void setProjectList(List<ProjectListEntity> list, boolean isLastPage) {
        RecyclerRefreshing();
        if (isLoadMore) {
            if (list != null && list.size() > 0) {
                projectadapter.addDatas(list);
            }
            mBinding.recyclerView.setNoMore(isLastPage);
        } else {
            if (list != null && list.size() > 0) {
                mBinding.emptyView.setVisibility(View.GONE);
                mBinding.recyclerView.setVisibility(View.VISIBLE);
                projectadapter.setDatas(list);
                mBinding.recyclerView.scrollToPosition(0);
                mBinding.recyclerView.setNoMore(isLastPage);
            } else {
                projectadapter.clearDatas();
                mBinding.emptyView.setVisibility(View.VISIBLE);
                mBinding.recyclerView.setVisibility(View.GONE);
            }
        }
    }

    /**
     * 材料采购
     *
     * @param data
     * @param isLastPage
     */
    public void setMaterialList(List<MaterialListEntity> data, boolean isLastPage) {
        RecyclerRefreshing();
        if (isLoadMore) {
            if (data != null && data.size() > 0) {
                materialsadapter.addDatas(data);
            }
            mBinding.recyclerView.setNoMore(isLastPage);
        } else {
            if (data != null && data.size() > 0) {
                mBinding.emptyView.setVisibility(View.GONE);
                mBinding.recyclerView.setVisibility(View.VISIBLE);
                materialsadapter.setDatas(data);
                mBinding.recyclerView.scrollToPosition(0);
                mBinding.recyclerView.setNoMore(isLastPage);
            } else {
                materialsadapter.clearDatas();
                mBinding.emptyView.setVisibility(View.VISIBLE);
                mBinding.recyclerView.setVisibility(View.GONE);
            }
        }
    }

    /**
     * 租赁需求
     *
     * @param data
     * @param isLastPage
     */
    public void setLeaseNeedList(List<LeaseNeedListEntity> data, boolean isLastPage) {
        RecyclerRefreshing();
        if (isLoadMore) {
            if (data != null && data.size() > 0) {
                leaseneedadapter.addDatas(data);
            }
            mBinding.recyclerView.setNoMore(isLastPage);
        } else {
            if (leaseneedadapter.getDatas().size()>0){
                leaseneedadapter.clearDatas();
            }
            if (data != null && data.size() > 0) {
                mBinding.emptyView.setVisibility(View.GONE);
                mBinding.recyclerView.setVisibility(View.VISIBLE);
                leaseneedadapter.setDatas(data);
                mBinding.recyclerView.scrollToPosition(0);
                mBinding.recyclerView.setNoMore(isLastPage);
            } else {
                leaseneedadapter.clearDatas();
                mBinding.emptyView.setVisibility(View.VISIBLE);
                mBinding.recyclerView.setVisibility(View.GONE);
            }
        }
    }

    /**
     * 劳务用工
     *
     * @param list
     * @param isLastPage
     */
    public void setLabourWorkList(List<LabourWorkListEntity> list, boolean isLastPage) {
        RecyclerRefreshing();
        if (isLoadMore) {
            if (list != null && list.size() > 0) {
                labourworkadapter.addDatas(list);
            } else {
                mBinding.recyclerView.setNoMore(isLastPage);
            }
        } else {
            if (list != null && list.size() > 0) {
                mBinding.emptyView.setVisibility(View.GONE);
                mBinding.recyclerView.setVisibility(View.VISIBLE);
                labourworkadapter.setDatas(list);
                mBinding.recyclerView.scrollToPosition(0);
                mBinding.recyclerView.setNoMore(isLastPage);
            } else {
                labourworkadapter.clearDatas();
                mBinding.emptyView.setVisibility(View.VISIBLE);
                mBinding.recyclerView.setVisibility(View.GONE);
            }
        }
    }

    /**
     * 分包计划
     *
     * @param data
     * @param isLastPage
     */
    public void setSubpackagePlanList(List<SubpackagePlanListEntity> data, boolean isLastPage) {
        RecyclerRefreshing();
        if (isLoadMore) {
            if (data != null && data.size() > 0) {
                subpackageplanadapter.addDatas(data);
            }
            mBinding.recyclerView.setNoMore(isLastPage);
        } else {
            if (data != null && data.size() > 0) {
                mBinding.emptyView.setVisibility(View.GONE);
                mBinding.recyclerView.setVisibility(View.VISIBLE);
                subpackageplanadapter.setDatas(data);
                mBinding.recyclerView.scrollToPosition(0);
                mBinding.recyclerView.setNoMore(isLastPage);
            } else {
                subpackageplanadapter.clearDatas();
                mBinding.emptyView.setVisibility(View.VISIBLE);
                mBinding.recyclerView.setVisibility(View.GONE);
            }
        }
    }

    private void mTitlesData() {
        for (int i = 0; i < mTitles.length; i++) {
            mBinding.tabLayout.addTab(mBinding.tabLayout.newTab());
            mBinding.tabLayout.getTabAt(i).setCustomView(R.layout.tab_item_text_image_view);
            View view = mBinding.tabLayout.getTabAt(i).getCustomView();
            TextView tv = view.findViewById(R.id.tv_tab_item);
            ImageView imageView = view.findViewById(R.id.image_tab_item);
            if (i == 0) {
                PickCopyUtils.setDemandForecastModel(0, imageView, true, getActivity());
                tv.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                tv.setTextColor(getResources().getColor(R.color.color_main));
                tv.invalidate();
            } else {
                PickCopyUtils.setDemandForecastModel(i, imageView, false, getActivity());
                tv.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                tv.setTextColor(getResources().getColor(R.color.color_tab_one));
                tv.invalidate();
            }
            tv.setText(mTitles[i]);
        }
        mBinding.tabLayout.getTabAt(0).select();
    }

    private void RecyclerRefreshing() {
        mBinding.recyclerView.refreshComplete();
        mBinding.recyclerView.loadMoreComplete();
    }

    private void initializePopView() {
        //地区
        initAddressPopup();
        //综合排序
        mBinding.sort.initPopup(mBinding.grayLayout, mBinding.popLine, this);
        //工程类型
        mBinding.projectType.initPopup(mBinding.grayLayout, mBinding.popLine, this);
        //发布时间
        mBinding.startTime.initPopup(mBinding.grayLayout, mBinding.popLine, this);
        //类型
        mBinding.type.initPopup(mBinding.grayLayout, mBinding.popLine, this);
        //类型处理
        initCateGoryPopup();
        mBinding.type.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showCategoryPop();
            }
        });
    }

    private void initCateGoryPopup() {
        popCategory = new ZCThreeLevelFilterWindow(this);
        popCategory.setBgView(mBinding.grayLayout);
        popCategory.setOnResultClick(new ZCThreeLevelFilterWindow.OnResultClick() {
            @Override
            public void result(Object key1, Object key2, Object key3) {
                CommonPopEntity pickResultProject1 = (CommonPopEntity) key1;
                CommonPopEntity pickResultProject2 = (CommonPopEntity) key2;
                CommonPopEntity pickResultProject3 = (CommonPopEntity) key3;
                switch (position) {
                    case 1://材料
                        if (pickResultProject1 != null && !TextUtils.isEmpty(pickResultProject1.getTypeName())) {
                            if (!pickResultProject1.getTypeName().equals("全部")) {
                                typeName1 = pickResultProject1.getTypeName();
                                mNo1 = pickResultProject1.getId();
                                requestBean.setCategoryId(mNo1);
                            } else {
                                typeName1 = "";
                                mNo1 = "";
                                mNo2 = "";
                                mNo3 = "";
                                requestBean.setCategoryId("");
                                popCategory.setData(commonlist, 0);
                            }
                        }
                        if (pickResultProject2 != null && !TextUtils.isEmpty(pickResultProject2.getTypeName())) {
                            if (!pickResultProject2.getTypeName().equals("全部")) {
                                typeName1 = pickResultProject2.getTypeName();
                                mNo2 = pickResultProject2.getId();
                                requestBean.setCategoryId(mNo2);
                            }
                        }
                        if (pickResultProject3 != null && !TextUtils.isEmpty(pickResultProject3.getTypeName())) {
                            if (!pickResultProject3.getTypeName().equals("全部")) {
                                typeName1 = pickResultProject3.getTypeName();
                                mNo3 = pickResultProject3.getId();
                                requestBean.setCategoryId(mNo3);
                            }
                        }
                        mBinding.type.updateChooseTextStatus(1, TextUtils.isEmpty(typeName1) ? mBinding.type.getKeyName() : typeName1);
                        break;
                    case 2://机械
                    case 3://设备
                        if (pickResultProject3 != null && !TextUtils.isEmpty(pickResultProject1.getTypeName())) {
                            if (!pickResultProject1.getTypeName().equals("全部")) {
                                typeName1 = pickResultProject1.getTypeName();
                                mNo1 = pickResultProject1.getNo();
                                requestBean.setCategoryId(mNo1);
                            } else {
                                typeName1 = "";
                                mNo1 = "";
                                mNo2 = "";
                                mNo3 = "";
                                requestBean.setCategoryId("");
                                popCategory.setData(commonlist, 0);
                            }
                        }
                        if (pickResultProject3 != null && !TextUtils.isEmpty(pickResultProject2.getTypeName())) {
                            if (!pickResultProject2.getTypeName().equals("全部")) {
                                typeName1 = pickResultProject2.getTypeName();
                                mNo2 = pickResultProject2.getNo();
                                requestBean.setCategoryId(mNo2);
                            }
                        }
                        mBinding.type.updateChooseTextStatus(1, TextUtils.isEmpty(typeName1) ? mBinding.type.getKeyName() : typeName1);
                        break;
                }
                //刷新数据
                initDataList();
            }

            @Override
            public void reset() {
                mNo1 = "";
                mNo2 = "";
                mNo3 = "";
                requestBean.setCategoryId("");
                popCategory.setData(commonlist, 0);
                mBinding.type.updateChooseTextStatus(0, mBinding.type.getKeyName());
                //刷新数据
                initDataList();
            }
        });
        popCategory.setOnDismissListener(() -> {
            if (position == 1) {
                if (!TextUtils.isEmpty(mNo1) || !TextUtils.isEmpty(mNo2) || !TextUtils.isEmpty(mNo3)) {
                    mBinding.type.updateChooseTextStatus(1, null);
                } else {
                    mBinding.type.updateChooseTextStatus(0, mBinding.type.getKeyName());
                }
            } else {
                if (!TextUtils.isEmpty(mNo1) || !TextUtils.isEmpty(mNo2)) {
                    mBinding.type.updateChooseTextStatus(1, null);
                } else {
                    mBinding.type.updateChooseTextStatus(0, mBinding.type.getKeyName());
                }
            }
        });
    }

    //pop初始化数据
    @Override
    public void onLoadChooseData(String mKeyName) {
        switch (mKeyName) {
            case "综合排序":
                mModel.getSortType(this, position);
                break;
            case "工程类型":
                mModel.getProjectTypeList(this);
                break;
            case "使用时间":
            case "创建时间":
            case "开工时间":
                mModel.getTime(this);
                break;
        }
    }

    //pop点击事件
    @Override
    public void onChooseResult(String mKeyName, CommonPopEntity entity, Object... objects) {
        int position = (int) objects[0];
        String no = position == 0 ? "" : entity.getNo();
        String id = position == 0 ? "" : entity.getId();
        String name = position == 0 ? "" : entity.getTypeName();
        switch (mKeyName) {
            case "综合排序":
                if (!TextUtils.equals(no, sort)) {
                    sort = no;
                    requestBean.setSortMethod(sort);
                }
                break;
            case "工程类型":
                if (!TextUtils.equals(name, projectType)) {
                    projectType = name;
                    requestBean.setEngineeringType(projectType);
                }
                break;
            case "使用时间":
            case "创建时间":
            case "开工时间":
                if (!TextUtils.equals(no, startTime)) {
                    startTime = no;
                    requestBean.setPlanUseTime(startTime);
                }
                break;
        }
        //刷新数据
        initDataList();
    }

    //地区
    private void initAddressPopup() {
        mBinding.district.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (scrData == null || scrData.size() == 0) {
                    mModel.getAddressModel(DemandForecastActivity.this);
                    return;
                }
                mBinding.popLine.setVisibility(View.VISIBLE);
                if (!TextUtils.isEmpty(province) || !TextUtils.isEmpty(city)) {
                    mBinding.district.updateChooseTextStatus(-2, null);
                } else {
                    mBinding.district.updateChooseTextStatus(-1, mBinding.district.getKeyName());
                }
                mAddressPop.showPopwindow0(mBinding.popLine);
            }
        });
        mAddressPop = new ThreeLevelAreaWindow(getActivity());
        mAddressPop.setBgView(mBinding.grayLayout);
        mAddressPop.setMemberHeight();
        mAddressPop.setCitySingle();
        mAddressPop.setProvinceSingle();
        mAddressPop.setShowSimpleName(true);
        mAddressPop.setOnlyShowProAndCity(true);
        mAddressPop.setAllSingleSelect(true);
        mAddressPop.setThreeListIsGone(true);
        mAddressPop.setOnResultClick(new ThreeLevelAreaWindow.OnResultClickProxy() {
            @Override
            protected void result(GoodsSourceLocationEntity entity) {
                province = "";
                city = "";
                requestBean.setProvince(province);
                requestBean.setCity(city);
                mBinding.district.updateChooseTextStatus(0, mBinding.district.getKeyName());
                if (!TextUtils.isEmpty(entity.getProvinceNo())) {
                    province = entity.getProvinceNo();
                    requestBean.setProvince(province);
                    mBinding.district.updateChooseTextStatus(1, entity.getProvinceName());
                    if (!TextUtils.isEmpty(entity.getCityNo())) {
                        city = entity.getCityNo();
                        requestBean.setCity(city);
                        mBinding.district.updateChooseTextStatus(1, entity.getCityName());
                    }
                }
                //刷新数据
                initDataList();
            }
        });
        mAddressPop.setOnDismissListener(() -> {
            if (!TextUtils.isEmpty(province) || !TextUtils.isEmpty(city)) {
                mBinding.district.updateChooseTextStatus(1, null);
            } else {
                mBinding.district.updateChooseTextStatus(0, mBinding.district.getKeyName());
            }
        });
    }

    /**
     * 地址列表信息
     *
     * @param data
     */
    public void setAddressList(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> data) {
        if (data != null && data.size() > 0) {
            scrData = data;
            String ProvinceCode = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.AREA_CODE, "");
            if (!TextUtils.isEmpty(ProvinceCode)) {
                mAddressPop.setData(data, true, ProvinceCode);
            } else {
                mAddressPop.setData(data, true);
            }
            mBinding.popLine.setVisibility(View.VISIBLE);
            if (!TextUtils.isEmpty(province) || !TextUtils.isEmpty(city)) {
                mBinding.district.updateChooseTextStatus(-2, null);
            } else {
                mBinding.district.updateChooseTextStatus(-1, mBinding.district.getKeyName());
            }
            mAddressPop.showPopwindow0(mBinding.popLine);
        }
    }

    /**
     * 综合排序
     *
     * @param datas
     */
    public void setSortTypeList(List<CommonPopEntity> datas) {
        setChooseItemData(1, datas);
    }

    /**
     * 工程类型
     *
     * @param data
     */
    public void setProjectTypeList(List<CommonPopEntity> data) {
        setChooseItemData(2, data);
    }

    /**
     * 时间
     *
     * @param data
     */
    public void setTime(List<CommonPopEntity> data) {
        setChooseItemData(3, data);
    }

    //弹窗信息
    public void setChooseItemData(int type, List<CommonPopEntity> data) {
        if (data == null || data.size() == 0) return;
        switch (type) {
            case 1://排序方式
                mBinding.sort.setData(data, sort);
                break;
            case 2://工程类别
                mBinding.projectType.setData(data, projectType);
                break;
            case 3://发布时间
                mBinding.startTime.setData(data, startTime);
                break;
        }
    }

    //类型-商品类型
    public void setMaterialType(List<CommonPopEntity> data) {
        this.commonlist = data;
        popCategory.setShowThreeRecycler(true);
        popCategory.setData(data, 0);
    }

    //类型-机械类型
    public void setMechanicalType(List<CommonPopEntity> data) {
        this.commonlist = data;
        popCategory.setShowThreeRecycler(false);
        popCategory.setData(data, 0);
    }

    //类型-设备类型
    public void setEquipmentType(List<CommonPopEntity> data) {
        this.commonlist = data;
        popCategory.setShowThreeRecycler(false);
        popCategory.setData(data, 0);
    }

    //显示类型弹窗
    public void showCategoryPop() {
        mBinding.popLine.setVisibility(View.VISIBLE);
        if (!TextUtils.isEmpty(mNo1)) {
            mBinding.type.updateChooseTextStatus(-2, null);
        } else {
            mBinding.type.updateChooseTextStatus(-1, mBinding.type.getKeyName());
        }
        popCategory.showPopwindow0(mBinding.popLine);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

}
